package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BFeeModelEnum;
import cn.huiyunche.base.service.enums.BFeeTypeEnum;
import cn.huiyunche.base.service.interfaces.BFeeModelService;
import cn.huiyunche.base.service.interfaces.BFeeTypeService;
import cn.huiyunche.base.service.interfaces.SBaseFeeService;
import cn.huiyunche.base.service.interfaces.SKmFeeService;
import cn.huiyunche.base.service.mappers.SKmFeeMapper;
import cn.huiyunche.base.service.model.BFeeType;
import cn.huiyunche.base.service.model.SBaseFee;
import cn.huiyunche.base.service.model.SKmFee;
import cn.huiyunche.base.service.model.SKmFeeExample;
import cn.huiyunche.base.service.vo.SOrderFeeDetailVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;

/**
 * @FileName: cn.huiyunche.app.service.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/16 下午3:46
 */
@Service
public class SKmFeeServiceImpl implements SKmFeeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SKmFeeServiceImpl.class);

    @Autowired
    private SKmFeeMapper sKmFeeMapper;

    @Autowired
    private BFeeTypeService bFeeTypeService;

    @Autowired
    private BFeeModelService bFeeModelService;

    @Autowired
    private SBaseFeeService sBaseFeeService;

    @Override
    public List<SKmFee> getList(String areaCode, BigDecimal endRange) {
        LOGGER.info("getList params : {}, {}", areaCode, endRange);
        if (StringUtils.isBlank(areaCode)) {
            LOGGER.error("getList param areaCode must not be null");
            throw new IllegalArgumentException("区域编码不能为空");
        }

        SKmFeeExample example = new SKmFeeExample();
        SKmFeeExample.Criteria criteria = example.createCriteria().andBAreaCodeEqualTo(areaCode);
        if (null != endRange) {
            criteria.andBeginRangeLessThan(endRange);
        }

        return sKmFeeMapper.selectByExample(example);
    }

    @Override
    public SOrderFeeDetailVo queryKmFee(String areaCode, BigDecimal distance) {
        LOGGER.info("queryKmFee params : {}, {}", areaCode, distance);
        if (StringUtils.isBlank(areaCode)) {
            LOGGER.error("queryKmFee param areaCode must not be null");
            throw new IllegalArgumentException("区域编码不能为空");
        }
        if (null == distance) {
            LOGGER.error("queryKmFee param distance must not be null");
            throw new IllegalArgumentException("里程不能为空");
        }

        //查询费用类型信息
        BFeeType feeType = bFeeTypeService.getByCode(bFeeModelService.getByCode(BFeeModelEnum.CITYWIDE_ORDER).getId(), Arrays.asList(BFeeTypeEnum.KM.getValue())).get(0);

        SOrderFeeDetailVo vo = new SOrderFeeDetailVo();
        vo.setId(feeType.getId());
        vo.setFeeCode(feeType.getFeeCode());
        vo.setFeeName(feeType.getFeeName());
        vo.setCost(BigDecimal.ZERO.setScale(2));
        vo.setActualAmount(vo.getCost());

        List<SKmFee> list = this.getList(areaCode, distance);
        if (CollectionUtils.isEmpty(list)) {
            LOGGER.error("queryStartingFee list is empty");
            throw new IllegalArgumentException("查询里程费失败");
        }

        //里程
        SKmFee sKmFee = list.get(0);

        //查询起步价配置
        SBaseFee baseFee = sBaseFeeService.getByAreaCode(areaCode);

        //计算里程费=（里程-起步里程*每公里单价
        BigDecimal km = distance.subtract(baseFee.getBaseKm());
        if (km.compareTo(BigDecimal.ZERO) == 1) {
            BigDecimal cost = km.multiply(sKmFee.getPricePerKm()).setScale(0, RoundingMode.UP).setScale(2);

            vo.setCost(cost);
            vo.setActualAmount(cost);
        }

        return vo;
    }
}
